package com.ruoyi.device.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.device.domain.SysDeviceType;
import com.ruoyi.device.service.ISysDeviceTypeService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/devicetype")
public class SysDeviceTypeController extends BaseController {
    @Autowired
    private ISysDeviceTypeService deviceTypeService;

    @PreAuthorize("@ss.hasPermi('device:devicetype:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDeviceType deviceType) {
        startPage();
//        if (SecurityUtils.hasPermi("device:devicetype:list")) {
//            System.out.println("当前用户有查看设备分类的权限");
//        }
//        if (SecurityUtils.hasPermi("device:devicetype:query")) {
//            System.out.println("当前用户有检索设备分类的权限");
//        }
//        if (SecurityUtils.hasPermi("device:devicetype:add")) {
//            System.out.println("当前用户有添加设备分类的权限");
//        }
//        if (SecurityUtils.hasPermi("device:devicetype:edit")) {
//            System.out.println("当前用户有修改设备分类的权限");
//        }
//        if (SecurityUtils.hasPermi("device:devicetype:remove")) {
//            System.out.println("当前用户有删除设备分类的权限");
//        }
//        if (SecurityUtils.hasPermi("device:devicetype:export")) {
//            System.out.println("当前用户有导出设备分类的权限");
//        }

        List<SysDeviceType> list = deviceTypeService.selectDeviceTypeList(deviceType);
        return getDataTable(list);
    }

    @GetMapping("/{typeId}")
    public AjaxResult getInfo(@PathVariable Long typeId) {
        startPage();
        SysDeviceType sysDeviceType = deviceTypeService.selectSysDeviceTypeById(typeId);
        return success(sysDeviceType);
    }

    /**
     * 新增设备
     */
    @PostMapping
    public AjaxResult add(@RequestBody SysDeviceType deviceType) {
        return toAjax(deviceTypeService.insertDeviceType(deviceType));
    }

    /**
     * 修改设备
     */
    @PutMapping
    public AjaxResult update(@RequestBody SysDeviceType deviceType) {
        return toAjax(deviceTypeService.updateDeviceType(deviceType));
    }

    /**
     * 删除单个设备
     */
    @DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable Long[] typeIds) {
        return toAjax(deviceTypeService.deleteDeviceTypeByIds(typeIds));
    }

    /**
     * 导出生成的设备类别列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDeviceType sysDeviceType)
    {
        List<SysDeviceType> list = deviceTypeService.selectDeviceTypeList(sysDeviceType);
        ExcelUtil<SysDeviceType> util = new ExcelUtil<SysDeviceType>(SysDeviceType.class);
        util.exportExcel(response, list, "生成的设备类别数据");
    }

}
